/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoBusqueda;

/**
 *
 * @author Estudiante_2
 */
public class ClienteListar extends javax.swing.JFrame {

    
     private final ClienteLnLocal clienteControl;
    private DefaultTableModel modelo;
   private List<Cliente>lista;
    private JFrame ventana;
    
    
    
    /**
     * 
     * Creates new form ClienteListar
     */
    public ClienteListar(ClienteLnLocal clienteControl) {
        this.clienteControl=clienteControl;
        
        initComponents();
        String nombres[] = {"id", "nombre", "telefono", "direccion", "sexo"};
        
         modelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        jTable2.setModel(modelo);      
        
        lista=clienteControl.getTodos();
        for (Cliente p : lista) {
            Object renglon[] = new Object[6];
            renglon[0] = p.getId() + "";
            renglon[1] = p.getnombre();
            renglon[2] = p.getTelefono() + "";
            renglon[3] = p.getDireccion();
            renglon[4] = p.getSexo();
            modelo.addRow(renglon);
        }
    }
    
    
     private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                     
        // TODO add your handling code here:
        List<Cliente> tmp= new ArrayList<Cliente>();
        if(jRadioButton1.isSelected()){
           tmp= clienteControl.getPorNombre(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda.CONTIENEC);  
        }else{
            tmp= clienteControl.getPorNombre(jTextField1.getText(), TipoBusqueda.INICIAC);  
        }
        String nombres[] = {"id", "nombre", "telefono", "direccion", "sexo"};
        modelo = new DefaultTableModel(nombres, 0);
        for (Cliente p : tmp) {
            String renglon[] = new String[5];
             renglon[0] = p.getId() + "";
            renglon[1] = p.getnombre();
            renglon[2] = p.getTelefono() + "";
            renglon[3] = p.getDireccion();
            renglon[4] = p.getSexo();
            modelo.addRow(renglon);
        }
        jTable2.setModel(modelo);
        lista=tmp;
    }
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
      // TODO add your handling code here:
         if(evt.getClickCount()==2){
            System.out.println(jTable2.getSelectedRow());
            Cliente p =lista.get(jTable2.getSelectedRow());
            
            if(ventana==null){
                ventana=new DialogoCli(p, clienteControl);
            }else{
                ventana.dispose();
                ventana=new DialogoCli(p, clienteControl);
            }
            ventana.setVisible(true);
        }
                         
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable2);

        jRadioButton1.setText("contiene");

        jRadioButton2.setText("inicia");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("atras");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addGap(43, 43, 43)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addComponent(jButton1)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(jRadioButton2)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jButton1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
